package com.kkb.controller;

import com.kkb.pojo.AdminRole;
import com.kkb.pojo.Admins;
import com.kkb.service.AdminRoleService;
import com.kkb.service.AdminService;
import com.kkb.pojo.LoginData;
import com.kkb.vo.LoginNameAndPwd;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("pages")
public class LoginController {
    @Resource
    private AdminService adminService;
    @Resource
    private AdminRoleService adminRoleService;

    @ResponseBody
    @RequestMapping(value = "adminLogin",method = RequestMethod.POST)
    public LoginData<Admins> login(HttpServletRequest request, HttpServletResponse response){
        //获取前端页面数据
        String loginName = request.getParameter("loginName");
        String pwd = request.getParameter("pwd");

        //传递数据参数
        LoginNameAndPwd lnp = new LoginNameAndPwd();
        lnp.setLogName(loginName);
        lnp.setPwd(pwd);

        //调取service,比较数据库中的数据
        Admins admin = adminService.selectByLogNameAndPwd(lnp);

        LoginData<Admins> adminsLoginData = new LoginData<>();

        if (admin !=null){
            adminsLoginData.setObj(admin);
            adminsLoginData.setMsg("登录成功~");
            System.out.println("登录成功！");
            request.getSession().setAttribute("adminInfo",admin);

        }else {
            adminsLoginData.setStatus(-1);
            adminsLoginData.setMsg("登录失败，请检查用户名和密码");
        }

        return adminsLoginData;
    }

    @ResponseBody
    @RequestMapping(value = "getInfo",method = RequestMethod.POST)
    public AdminRole getInfo(HttpServletRequest request, HttpServletResponse response){
        //获取登录信息的session值
        Admins adminInfo = (Admins) request.getSession().getAttribute("adminInfo");

        AdminRole RoleInfo = adminRoleService.findById(adminInfo.getRoleId());

        return RoleInfo;
    }


}
